
import { Button, Form, Modal } from "@/components/base";
import * as apis from "@/apis";
import { useVisible } from "@/tools/hooks";
import React from "react";
import SelectUser from "@/components/select-user";

/** 安排PM */
export function ArrangePM(props: {
    record: any,
    index: number,
    refresh: () => Promise<void>,
}) {
    const { record, refresh } = props;
    const { onOpen, onClose, open } = useVisible();
    const [form] = Form.useForm();

    const handleOk = React.useCallback(async () => {
        const { pm } = form.getFieldsValue();
        await apis.order.planPm.post({ id: record.id, pmId: pm.userid, pmName: pm.name })
        onClose();
        refresh();
    }, [form, onClose, record.id, refresh])

    if (record.orderStatus !== apis.order.OrderStatus.待安排) return <></>;
    return (
        <>
            <Button type='link' onClick={onOpen} >安排PM</Button>
            <Modal title="安排PM" open={open} onOk={handleOk} onCancel={onClose}>
                <Form form={form} layout='vertical' >
                    <Form.Item label='PM' name='pm' rules={[{ required: true }]} >
                        <SelectUser />
                    </Form.Item>
                </Form>
            </Modal>
        </>
    )
}
